package com.eningqu.aipen.ui.mine;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.eningqu.aipen.demo.PenCtrlSdkManager;
import com.eningqu.aipen.demo.databinding.FragmentMineBinding;
import com.eningqu.aipen.logic.model.mine.DoctorBean;
import com.eningqu.aipen.logic.network.Apis;
import com.eningqu.aipen.logic.network.BaseRetrofitUtils;
import com.eningqu.aipen.logic.network.RxLifecycle;
import com.eningqu.aipen.logic.network.SchedulerTransformer;
import com.eningqu.aipen.ui.base.BaseFragment;
import com.eningqu.aipen.ui.base.TestActivity;
import com.eningqu.aipen.ui.login.LoginActivity;
import com.eningqu.aipen.utils.GlideUtils;
import com.eningqu.aipen.utils.GlobalConfig;
import com.eningqu.aipen.utils.SPUtil;
import com.kongzue.dialogx.dialogs.PopTip;

import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Package:com.eningqu.aipen.ui.mine
 * Author:starr
 * Time:2023/11/7 11:10
 * Description:
 */
public class MineFragment extends BaseFragment<FragmentMineBinding> {

    DoctorBean.EmrYsInfoDTO ysBean = new DoctorBean.EmrYsInfoDTO();

    public static MineFragment newInstance() {
        return new MineFragment();
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mBinding.setOnClick(new MoreClick());
        mBinding.setYs(ysBean);
    }


    @Override
    public void onStart() {
        super.onStart();
    }


    @SuppressLint("CheckResult")
    @Override
    protected void async() {
        Integer ysId = (Integer) SPUtil.getInstance().getData(GlobalConfig.ysId, -1);
        if (ysId == -1) {
            PopTip.tip("请先登录...");
        } else {
            BaseRetrofitUtils.getInstance().create(Apis.class).getDoctorInfo(ysId)
                    .compose(new SchedulerTransformer<>())
                    .compose(RxLifecycle.bindExLifecycle(this))
                    .subscribe(doctorBean -> {
                        //设置新的数据，自动刷新界面
                        mBinding.name.setText(doctorBean.emrYsInfo.emrYsSection);
                        mBinding.nameTv.setText(doctorBean.emrYsInfo.emrYsName);
                        GlideUtils.loadCircleImage(getContext(),doctorBean.emrYsInfo.emrYsImg,
                                mBinding.headIv);
                        SPUtil.getInstance().putData(GlobalConfig.ysName, doctorBean.emrYsInfo.emrYsName);
                        ysBean= doctorBean.emrYsInfo;
                    });
        }
    }

    @Override
    protected void initStatusBar() {
        mBinding.titleTopBar.baseTvTitle.setText("我的");
    }


    @SuppressWarnings("unused")
    public class MoreClick {
        //退出登录
        public void loginOut(View view) {
            SPUtil.getInstance().removeAll();
            PenCtrlSdkManager.getInstance().release();
            LoginActivity.startAction(getContext());
        }

        //跳转到文心一言
        public void gotoChat(View view) {
            ChatActivity.startAction(getContext());
        }

        //跳转到测试界面
        public void gotoTest(View view) {
            TestActivity.startAction(getContext());
        }

        //跳转到个人资料界面
        public void gotoPersonal(View view) {
            Bundle bundle=new Bundle();
            bundle.putSerializable("ysBean",ysBean);
            PersonalActivity.startAction(getContext(),bundle);
        }

        //版本信息
        public void version(View view) {
            PopTip.tip("当前版本Version 4.0");
        }

        //软件更新
        public void update(View view) {
            PopTip.tip("已经是最新版本");
        }

        //固件更新
        public void about(View view) {
            PopTip.tip("关于我们");
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mBinding = null;
    }
}
